﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyMvc.BLL;
using MyMvc.IBLL;
using MyMvc.Model.Models;
using System.Web.Mvc.Ajax;
using MyMvc.Model;

namespace BlueHome.Admin.Controllers
{
    
    public class AddArticleController : Controller
    {
        //
        // GET: /AddFile/
        IArticleService _articleService = new ArticleService();
        ICategoryTypeService _typeService = new CategoryTypeService();
        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// 实现添加信息
        /// </summary>
        /// <param name="art"></param>
        /// <returns></returns>
        public ActionResult AddArticle(Article art)
        {
            art.Content1 = Server.UrlDecode(art.Content1);
            art.CreateDateTime = DateTime.Now;
            //实现对Article表的添加信息
            _articleService.AddEntities(art);
            return Content("OK");
        }

        public ActionResult UpdataArticle(int ID)
        {
            //查询出当前的文档信息
            var currentArticle = _articleService.LoadEntities(c => c.ID == ID).FirstOrDefault();

            //做成一个强类型，传递给前台接收
            ViewData.Model = currentArticle;
            return View();
        }

        /// <summary>
        /// 修改文章信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdataArticleMethod(Article art)
        {
            art.Content1 = Server.UrlDecode(art.Content1);
            art.CreateDateTime = DateTime.Now;
            _articleService.UpdateEntities(art);
            return Content("OK");
        }

        /// <summary>
        /// 获取一级菜单数据
        /// </summary>
        public ActionResult GetCategoryList()
        {
            if (!Request.IsAjaxRequest())
            {
                return Content("出错了！");
            }
            List<CategoryType> modellist = new List<CategoryType>();
            modellist = _typeService.LoadEntities(c => true).ToList();
            List<int> list = new List<int>();
            foreach (var a in modellist)
            {
                list.Add(a.ParentId);
            }
            return Json(list,JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取某［一级菜单］的所有［二级菜单］数据
        /// </summary>
        public ActionResult GetTypeList(int id)
        {
            if (!Request.IsAjaxRequest())
            {
                return Content("出错了！");
            }
            List<CategoryType> modellist = new List<CategoryType>();
            modellist = _typeService.LoadEntities(c => c.ParentId == id).ToList();
            return Json(modellist,JsonRequestBehavior.AllowGet);
        }

    }
}
